package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.mapper.DailyContentMapper;
import cn.wolfcode.trip.base.mapper.DailyMapper;
import cn.wolfcode.trip.base.mapper.InformationMapper;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DailyServiceImpl implements IDailyService {

    @Autowired
    private DailyMapper dailyMapper;
    @Autowired
    private DailyContentMapper dailyContentMapper;
    @Autowired
    private InformationMapper informationMapper;


    public PageInfo<Daily> query(DailyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Daily> list = dailyMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public void saveOrUpdate(Daily daily) {
        if (daily.getId() == null) {
            dailyMapper.insert(daily);
            DailyContent dailyContent = daily.getDailyContent();
            dailyContent.setId(daily.getId());
            dailyContentMapper.insert(dailyContent);
        } else if (daily.getCoverUrl() == null) {
            //不上传图片则保留原有图片
            Daily daily1 = dailyMapper.selectByPrimaryKey(daily.getId());
            daily.setCoverUrl(daily1.getCoverUrl());
            dailyMapper.updateByPrimaryKey(daily);
            DailyContent dailyContent = daily.getDailyContent();
            dailyContent.setId(daily.getId());
            dailyContentMapper.updateByPrimaryKey(dailyContent);
        } else
            dailyMapper.updateByPrimaryKey(daily);
            DailyContent dailyContent = daily.getDailyContent();
            dailyContent.setId(daily.getId());
            dailyContentMapper.updateByPrimaryKey(dailyContent);
    }

    public DailyContent get(Long id) {
        return dailyContentMapper.selectByPrimaryKey(id);
    }

    public Daily getBigNews(Long id) {
        Daily daily = dailyMapper.selectByPrimaryKey(id);
        String json = daily.getJson();
        return daily;
    }


}
